package com.adkfp.action;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adkfp.utils.JsonUtil;
import com.adkfp.utils.LayuiUtil;

import net.sf.json.JSONArray;

import com.adkfp.bean.ADKActimeBean;
import com.adkfp.bean.ADKDeptBean;
import com.adkfp.bean.ADKPowerGroupBean;
import com.adkfp.bean.ADKPowerGroupDoorBean;
import com.adkfp.bean.ADKUserInfoBean;
import com.adkfp.common.ControllerTime;
import com.adkfp.common.PowerNodes;
import com.adkfp.service.IADKActimeService;
import com.adkfp.service.IADKPowerGroupService;
import com.adkfp.service.IADKUserInfoService;

/**
 * class name:ADKPowerGroupAction <BR>
 * class description: ADKPowerGroup表的增删改查 <BR>
 * Remark: <BR>
 * @version 1.00 2019年01月03日
 * @author JACK)jackwei
 */
@Controller
@RequestMapping("aDKPowerGroup")
public class ADKPowerGroupAction {
	@Resource(name="aDKPowerGroupService")
	private IADKPowerGroupService aDKPowerGroupService;

	@Resource(name="aDKUserInfoService")
	private IADKUserInfoService aDKUserInfoService;
	
	@Resource(name="aDKActimeService")
	private IADKActimeService aDKActimeService;
  /**
 	* Method name: selectAll <BR>
 	* Description: 查询所有记录 <BR>
 	* Remark: <BR>
 	* @param bean
 	* @return  Object<BR>
 	*/
	@RequestMapping("selectAll")
	@ResponseBody
	public Object selectAll(ADKPowerGroupBean bean) throws ClassNotFoundException, SQLException, IOException {
		/*List<ADKPowerGroupBean> list = new ArrayList<>();
		list = aDKPowerGroupService.findByCondition(bean);
		long count = aDKPowerGroupService.findByConditionCount(bean);
		
		return list;*/
		
		InputStream in =  this.getClass().getClassLoader().getResourceAsStream("/database.properties");
		Properties properties = new Properties();
		properties.load(in);
	

		//获取key对应的value值
		System.out.print("4444=================================");
		String dbdriver = properties.getProperty("jdbc.driverClass");//"com.mysql.jdbc.Driver");
		String dbhost =properties.getProperty("jdbc.jdbcUrl") ;//"jdbc:mysql://192.168.0.198:3306/qudao";
		String dbuser = properties.getProperty("jdbc.user","root");
		String dbpassword = properties.getProperty("jdbc.password", "123456");
		PowerNodes dt=new PowerNodes(dbdriver,dbhost,dbuser,dbpassword);
		return dt.getResult();
	}
	
	/**
	 * Method name: index <BR>
	 * Description: 转发到页面 <BR>
	 * Remark: <BR>
	 * @return  String<BR>
	 */
	@RequestMapping("index")
	public String index() {
		return "aDKPowerGroup/PowerGroupTree";
	}
	
	//跳转权限组-人员权限-增加人员页面
	@RequestMapping("addUserGroup")
	public String addUserGroup(String groupID,HttpServletRequest request,ADKPowerGroupBean prowergroup) {
		System.out.println("跳转权限组-人员权限-增加人员页面de gropuid 是----"+groupID);
		prowergroup.setGroupID(Integer.parseInt(groupID));
		request.setAttribute("prowerGroup", prowergroup);
		//return "aDKPowerGroup/addUserPowerGroup";
		return "aDKPowerGroup/addUserPowerGroup1";
	}
	
	//跳转权限组-门权限-增加门权限页面    查找通行时间组（下拉框）
	@RequestMapping("addUserGroup2")
	public String addUserGroup2(String groupID,HttpServletRequest request,ADKPowerGroupBean prowergroup) {
		System.out.println("跳转权限组-门权限-增加门权限页面de gropuid 是----"+groupID);
		prowergroup.setGroupID(Integer.parseInt(groupID));
		request.setAttribute("prowerGroup", prowergroup);
		//return "aDKPowerGroup/addUserPowerGroup";
		
		List<ADKActimeBean> list=new ArrayList<>();
		list =aDKActimeService.findtime();
			System.out.println("-----下拉框中----");
			//System.out.println(list);
			JSONArray object=JSONArray.fromObject(list);
			request.setAttribute("timelist", object);
		return "aDKPowerGroup/addMenPowerGroup";
	}
	
	
	//转发到右侧页面
	@RequestMapping("PowerGroupRight")
	public String PowerGroupRight(HttpServletRequest request,Integer id,HttpServletResponse response) {
		ADKPowerGroupBean pgBean = new ADKPowerGroupBean();	
		if(id != null && !id.equals(" ")){
			pgBean=aDKPowerGroupService.findById(id);
		}
		request.setAttribute("pgBean",pgBean);
		return "aDKPowerGroup/PowerGroupRight1";
	}
	
	
	//删除
	@RequestMapping("deletePowerGroup")
	@ResponseBody
	public String deletePowerGroup(String id) {
		System.out.println("----id----"+id);
		if(id!=null&&!id.equals("")&&!id.equals(1)) {
			aDKPowerGroupService.delete(Integer.parseInt(id));
		return "SUCCESS";
	  }else {
		return "ERROR";
		}
	}
	
	//跳转增加页面
	@RequestMapping("add")
	public String add() {
		return "aDKPowerGroup/addPowerGroup";
	}
	
	//增加
	@RequestMapping("insertPowerGroup")
	@ResponseBody
	public void insertPowerGroup(ADKPowerGroupBean netgroup) {
		System.err.println("----进入powergroup方法1----");
			aDKPowerGroupService.insert(netgroup);
		
	}
	
	
	/**
	 *转发页面到修改权限页面 
	 */
	@RequestMapping("updateByID")
	public String updateByID(HttpServletRequest request, String id,HttpServletResponse response) {
		ADKPowerGroupBean Powerbean = new ADKPowerGroupBean();	
		if(id != null && !id.equals("")){
			Powerbean=aDKPowerGroupService.findById(Integer.parseInt(id));
		}
		request.setAttribute("powerbean",Powerbean);
		return "aDKPowerGroup/updatePowerGroup";
	}
	
	
	
	//转发到右侧页面
	@RequestMapping("byID")
	public String byID(HttpServletRequest request, String id,ADKPowerGroupBean bean) {
		System.out.println("1111。。。。页面的groupID是"+id);
		bean.setGroupID(Integer.parseInt(id));
		request.setAttribute("power",bean);
		
		return "aDKPowerGroup/PowerGroupRight1";
	}
	
	//查找通行时间组（下拉框）
	@RequestMapping("findtime")
	//@ResponseBody
	public Object findtime(HttpServletRequest request,String autoID,ADKPowerGroupBean pgbean) {
	List<ADKActimeBean> list=new ArrayList<>();
	list =aDKActimeService.findtime();
		System.out.println("-----下拉框中----");
		//System.out.println(list);
		JSONArray object=JSONArray.fromObject(list);
		request.setAttribute("timelist", object);
		
		ADKPowerGroupDoorBean pGdoor = new ADKPowerGroupDoorBean();	
		pGdoor.setAutoID(Integer.parseInt(autoID));
		request.setAttribute("pGdoor",pGdoor);
		return "aDKPowerGroup/PowerGrouptime";
	  
	 
}
	
	
	//转发到右侧页面（人员权限）
		@RequestMapping("byID1")
		public String byID1(HttpServletRequest request, String id,ADKPowerGroupBean bean) {
			System.out.println("1111。。。。页面的groupID是"+id);
			bean.setGroupID(Integer.parseInt(id));
			request.setAttribute("power",bean);
			return "aDKUserGroup/aDKUserGroupRight";
		}
	
	
	//修改树形-权限组页面
	@RequestMapping("updatePGroup")
	@ResponseBody
	public void updatePGroup(ADKPowerGroupBean powergroup) {
		System.err.println("----修改   进入powergroup方法----");
			aDKPowerGroupService.update(powergroup);
	}
	
	
	
	@RequestMapping("selectAlluser")
	@ResponseBody
	public Object selectAlluser(ADKUserInfoBean bean,String groupID) {
		if(groupID.equals("")) {
			groupID="1";
		}
		System.out.println("----权限组中的groupID是----"+groupID);
		List<ADKUserInfoBean> list = new ArrayList<>();
		bean.setGroupID(Integer.parseInt(groupID));
		list = aDKUserInfoService.pfindByCondition(bean);
		long count = aDKUserInfoService.pfindByConditionCount(bean);
		return LayuiUtil.data(count, list);
	}


	
	
}